{extend name="common@admin/main" /}
{block name="content"}
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
            <div class="pull-right">
                <button type="submit" form="form-set" data-toggle="tooltip" title="保存" class="btn btn-primary"> <i class="fa fa-save"></i>
                </button>
                <a href="" data-toggle="tooltip" title="取消" class="btn btn-default"> <i class="fa fa-reply"></i>
                </a>
            </div>
            <h1>管理员</h1>
            <ul class="breadcrumb">
                <li>
                    <a href="">首页</a>
                </li>
                <li>
                    <a href="">管理员</a>
                </li>
            </ul>
        </div>
    </div>
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">
                    <i class="fa fa-pencil"></i>
                    设置管理员
                </h3>
            </div>
            <div class="panel-body">
                <!-- __ACTION__ -->
                <form action="" method="post" enctype="multipart/form-data" id="form-set" class="form-horizontal">

                    <ul class="nav nav-tabs">
                        <li class="active">
                            <a href="#tab-general" data-toggle="tab">基本信息</a>
                        </li>
                    </ul>
                    <div class="tab-content">
                        <div class="tab-pane active" id="tab-general">

                            <div class="form-group">
                                <label class="col-sm-2 control-label" for="input-username">用户名</label>
                                <div class="col-sm-10">
                                    <input type="text" name="username" value="" placeholder="用户名" id="input-username" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                    <label class="col-sm-2 control-label" for="input-email">邮箱</label>
                                    <div class="col-sm-10">
                                        <input type="text" name="email" value="" placeholder="邮箱" id="input-email" class="form-control">
                    
                                    </div>
                                </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label" for="input-password">密码</label>
                                <div class="col-sm-10">
                                    <input type="text" name="password" value="" placeholder="密码" id="input-password" class="form-control">
                                </div>
                            </div>
                            

                            <div class="form-group">
                                <label class="col-sm-2 control-label" for="input-password_salt">角色</label>
                                <div class="col-sm-10">
                                    <div class="row">

                                    </div>
                                </div>
                            </div>

                        </div>

                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="title"}
<title>角色授权</title>
{/block}

{block name="appendJS"}
<script>
    $(function(){
        var user={$user},
            role={$role},
            allrole={$allrole},
            back_user={$session_user};
            console.log(user)    
            console.log(role)    
            console.log(allrole)
            // console.log(back_user)
            
            //角色列表
            var div=$('<div/>',{class:"col-sm-2"});   
            //如果前台有返回数据说明是修改，将需要修改的ID放入隐藏域
            $('.form-horizontal').append($('<input/>',{
                      type:"hidden",
                      name:"user_id",
                      id:"input-user_id",
                      value:user.id,
                  }))

            checkbox_list(allrole,div,role)

            //回显用户信息与所属角色信息
            var username=$(':text[name="username"]').val(user.username),
                password=$(':text[name="password"]').val(user.password),
                email=$(':text[name="email"]').val(user.email);
                
                username.attr("disabled","false");
                password.attr("disabled","false");
                email.attr("disabled","false");

            var user_id=user.id;
            insert_save(user_id);
  
    }) 
 //遍历复选框
 function  checkbox_list(allrole,div,role){
      $.each(allrole,function(key,val){
          var input=$('<input/>',{
               type:"checkbox",
               name:val,
               value:val.id,
               class:"form-control", 
               id:"input-role-"+val.id});
          var label=$('<label/>',{for:"input-role-"+val.id}).html(val.name);
          
          if(role!=null&&role.id==val.id){
             input.prop('checked',true);
          }  
          div.append(input).append(label);
     });
      $('.row').append(div);
 }   
 //数据保存
 function insert_save(id=""){
        //提交修改
        var submit=$('.fa-save');
        var username=$(':text[name="username"]').val(),
            password=$(':text[name="password"]').val(),
            email=$(':text[name="email"]').val();
        var role_id=$(':checkbox:checked').val();
        console.log(role_id)
        if(id!=""){
            var urls='/admin/updateRole.json',
                send={role_id:role_id,user_id:id,};
        }else{
            var urls='/admin/addRole.json',
                send={role_id:role_id,username:username,password:password,email:email};
        }
        
        submit.click(function(){
           $.post(urls,send,function(res){
                 // if(res.code==1){
                 //    //跳转用户列表
                 //    location.href="/admin/permiss/userlist";
                 // }else{
                 //    alert('添加失败');
                 // }
           });
           return false;
        })
 }

</script>
{/block}
